<?php
//按钮组件模型
namespace App\Models\Traits;
//trait和类类似，却不能实例化，只能被继承
trait Btn
{
    //当前用户是否有权限
    private function checkAuth(string $route_name)
    {
        if (!stristr(request()->url(), '/api/')) {
            //超级管理员不受权限控制
            if (request()->username != 'admin' && !in_array($route_name, request()->nodeList)) {
                return false;
            }
        }
        return true;
    }
    //查看按钮
    public function showBtn(string $route_name, $params = []): string
    {
        if (!$this->checkAuth($route_name)) return '';
        return "<a href='" . route($route_name, $params) . "'class='label label-success'>查看</a>";
    }

    //添加按钮
    public function addBtn(string $route_name, string $name, $params = []): string
    {
        if (!$this->checkAuth($route_name)) return '';
        return "<a href='" . route($route_name, $params) . "' class='btn btn-primary radius'>
            <i class='Hui-iconfont'>&#xe600;</i>" . $name . "</a>";
    }

    //修改按钮
    public function editBtn(string $route_name, string $name, $params = []): string
    {
        if (!$this->checkAuth($route_name)) return '';
        return "<a href='" . route($route_name, $params) . "'class='label label-primary'>" . $name . "</a>";
    }

    //删除按钮
    public function delBtn(string $route_name, string $name, $params = []): string
    {
        if (!$this->checkAuth($route_name)) return '';
        return "<a href='" . route($route_name, $params) . "'class='label label-danger delBtn'>" . $name . "</a>";
    }

    //批量删除按钮
    public function delAllBtn(string $route_name, string $name, $params = []): string
    {
        if (!$this->checkAuth($route_name)) return '';
        return "<a onclick=\"datadel()\" class=\"btn btn-danger radius\">
                   <i class=\"Hui-iconfont\">&#xe62e;</i>批量删除
                   </a>";
    }

    //恢复
    public function resBtn(string $route_name, string $name, $params = []): string
    {
        if (!$this->checkAuth($route_name)) return '';
        return "<a href='" . route($route_name, $params) . "'class='label label-success'>" . $name . "</a>";
    }
}